﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace He.DAL
{
	/// <summary>
	/// 数据访问类:job
	/// </summary>
	public partial class job
	{
		public job()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("id", "he_job"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from he_job");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(He.Model.job model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into he_job(");
			strSql.Append("title,vacancies,location,treatment,content,add_time,end_time,is_lock,sort_id,lang,attr_str,attr_str2,attr_num)");
			strSql.Append(" values (");
			strSql.Append("@title,@vacancies,@location,@treatment,@content,@add_time,@end_time,@is_lock,@sort_id,@lang,@attr_str,@attr_str2,@attr_num)");
			strSql.Append(";select LAST_INSERT_ROWID()");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@vacancies", DbType.String),
					new SQLiteParameter("@location", DbType.String),
					new SQLiteParameter("@treatment", DbType.String),
					new SQLiteParameter("@content", DbType.String),
					new SQLiteParameter("@add_time", DbType.String),
					new SQLiteParameter("@end_time", DbType.String),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@lang", DbType.String),
					new SQLiteParameter("@attr_str", DbType.String),
					new SQLiteParameter("@attr_str2", DbType.String),
					new SQLiteParameter("@attr_num", DbType.Int32,8)};
			parameters[0].Value = model.title;
			parameters[1].Value = model.vacancies;
			parameters[2].Value = model.location;
			parameters[3].Value = model.treatment;
			parameters[4].Value = model.content;
			parameters[5].Value = model.add_time;
			parameters[6].Value = model.end_time;
			parameters[7].Value = model.is_lock;
			parameters[8].Value = model.sort_id;
			parameters[9].Value = model.lang;
			parameters[10].Value = model.attr_str;
			parameters[11].Value = model.attr_str2;
			parameters[12].Value = model.attr_num;

			object obj = DbHelperSQLite.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(He.Model.job model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update he_job set ");
			strSql.Append("title=@title,");
			strSql.Append("vacancies=@vacancies,");
			strSql.Append("location=@location,");
			strSql.Append("treatment=@treatment,");
			strSql.Append("content=@content,");
			strSql.Append("add_time=@add_time,");
			strSql.Append("end_time=@end_time,");
			strSql.Append("is_lock=@is_lock,");
			strSql.Append("sort_id=@sort_id,");
			strSql.Append("lang=@lang,");
			strSql.Append("attr_str=@attr_str,");
			strSql.Append("attr_str2=@attr_str2,");
			strSql.Append("attr_num=@attr_num");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@vacancies", DbType.String),
					new SQLiteParameter("@location", DbType.String),
					new SQLiteParameter("@treatment", DbType.String),
					new SQLiteParameter("@content", DbType.String),
					new SQLiteParameter("@add_time", DbType.String),
					new SQLiteParameter("@end_time", DbType.String),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@lang", DbType.String),
					new SQLiteParameter("@attr_str", DbType.String),
					new SQLiteParameter("@attr_str2", DbType.String),
					new SQLiteParameter("@attr_num", DbType.Int32,8),
					new SQLiteParameter("@id", DbType.Int32,8)};
			parameters[0].Value = model.title;
			parameters[1].Value = model.vacancies;
			parameters[2].Value = model.location;
			parameters[3].Value = model.treatment;
			parameters[4].Value = model.content;
			parameters[5].Value = model.add_time;
			parameters[6].Value = model.end_time;
			parameters[7].Value = model.is_lock;
			parameters[8].Value = model.sort_id;
			parameters[9].Value = model.lang;
			parameters[10].Value = model.attr_str;
			parameters[11].Value = model.attr_str2;
			parameters[12].Value = model.attr_num;
			parameters[13].Value = model.id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_job ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_job ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.job GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,title,vacancies,location,treatment,content,add_time,end_time,is_lock,sort_id,lang,attr_str,attr_str2,attr_num from he_job ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			He.Model.job model=new He.Model.job();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["title"]!=null && ds.Tables[0].Rows[0]["title"].ToString()!="")
				{
					model.title=ds.Tables[0].Rows[0]["title"].ToString();
				}
				if(ds.Tables[0].Rows[0]["vacancies"]!=null && ds.Tables[0].Rows[0]["vacancies"].ToString()!="")
				{
					model.vacancies=ds.Tables[0].Rows[0]["vacancies"].ToString();
				}
				if(ds.Tables[0].Rows[0]["location"]!=null && ds.Tables[0].Rows[0]["location"].ToString()!="")
				{
					model.location=ds.Tables[0].Rows[0]["location"].ToString();
				}
				if(ds.Tables[0].Rows[0]["treatment"]!=null && ds.Tables[0].Rows[0]["treatment"].ToString()!="")
				{
					model.treatment=ds.Tables[0].Rows[0]["treatment"].ToString();
				}
				if(ds.Tables[0].Rows[0]["content"]!=null && ds.Tables[0].Rows[0]["content"].ToString()!="")
				{
					model.content=ds.Tables[0].Rows[0]["content"].ToString();
				}
				if(ds.Tables[0].Rows[0]["add_time"]!=null && ds.Tables[0].Rows[0]["add_time"].ToString()!="")
				{
					model.add_time=ds.Tables[0].Rows[0]["add_time"].ToString();
				}
				if(ds.Tables[0].Rows[0]["end_time"]!=null && ds.Tables[0].Rows[0]["end_time"].ToString()!="")
				{
					model.end_time=ds.Tables[0].Rows[0]["end_time"].ToString();
				}
				if(ds.Tables[0].Rows[0]["is_lock"]!=null && ds.Tables[0].Rows[0]["is_lock"].ToString()!="")
				{
					model.is_lock=int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString());
				}
				if(ds.Tables[0].Rows[0]["sort_id"]!=null && ds.Tables[0].Rows[0]["sort_id"].ToString()!="")
				{
					model.sort_id=int.Parse(ds.Tables[0].Rows[0]["sort_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["lang"]!=null && ds.Tables[0].Rows[0]["lang"].ToString()!="")
				{
					model.lang=ds.Tables[0].Rows[0]["lang"].ToString();
				}
				if(ds.Tables[0].Rows[0]["attr_str"]!=null && ds.Tables[0].Rows[0]["attr_str"].ToString()!="")
				{
					model.attr_str=ds.Tables[0].Rows[0]["attr_str"].ToString();
				}
				if(ds.Tables[0].Rows[0]["attr_str2"]!=null && ds.Tables[0].Rows[0]["attr_str2"].ToString()!="")
				{
					model.attr_str2=ds.Tables[0].Rows[0]["attr_str2"].ToString();
				}
				if(ds.Tables[0].Rows[0]["attr_num"]!=null && ds.Tables[0].Rows[0]["attr_num"].ToString()!="")
				{
					model.attr_num=int.Parse(ds.Tables[0].Rows[0]["attr_num"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,title,vacancies,location,treatment,content,add_time,end_time,is_lock,sort_id,lang,attr_str,attr_str2,attr_num ");
			strSql.Append(" FROM he_job ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM he_job ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from he_job T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "he_job";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

